#include "D3D9MaterialRender.h"
#include "D3D9Device.h"

//! sharpShooter
namespace sharpShooter
{
	//! Render
	namespace Render
	{
		void CD3D9MaterialRender::onRender(IMaterial &material)
		{
			IDirect3DDevice9 *pD3D9 = dynamic_cast<Device::CD3D9Device*>(m_SharpShooterDevice)->getDevice();
			EMaterialType materialType = material.getMaterialType();
			switch (materialType)
			{
			case MT_POINT_CLOUD:
				pD3D9->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT);
				break;
			case MT_WIRE_FRAME:
				pD3D9->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
				break;
			case MT_SOLID:
			case MT_NORMAL_MAP:
				pD3D9->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
				break;
			default:	;

			}

			pD3D9->SetRenderState(D3DRS_LIGHTING, material.getEnableLighting());
			pD3D9->SetRenderState(D3DRS_ZENABLE, material.getEnableZWriting());
			pD3D9->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE,
				material.getEnableBackFaceCulling() && material.getEnableFrontFaceCulling());
			if ( !material.getEnableBackFaceCulling() && !material.getEnableFrontFaceCulling() )
			{
				pD3D9->SetRenderState(D3DRS_FILLMODE, D3DCULL_NONE);
			}
			else if ( material.getEnableFrontFaceCulling() )
			{
				pD3D9->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
			}
			pD3D9->SetRenderState(D3DRS_FOGENABLE, material.getEnableFog());
			
			pD3D9->SetRenderState(D3DRS_ALPHATESTENABLE, material.getEnableAlpha());
			pD3D9->SetRenderState(D3DRS_ALPHABLENDENABLE, material.getEnableAlpha());
			

			pD3D9->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
			pD3D9->SetRenderState(D3DRS_ALPHAFUNC,D3DCMP_GREATER);

		}
	}
}